# coding=utf-8

from typing import Match
import click
import sys
import os
from pathlib import Path
from click.types import File
from click.utils import open_file


def make_file(file_name, data_source: list = list()) -> None:
    writeable = "\n".join(data_source)
    with open(file_name, "w") as file:
        file.write(writeable)
    

@click.command()
@click.option("--file", "-f", help="请输入要格式化的文件")
@click.option("--mode", "-m", default="all", show_default=True, type=click.Choice(['pod', 'dependency', 'all'], case_sensitive=False), help="选择您要使用的模式")
def imp2pod(file, mode):
    pwd = os.getcwd()

    exclude_arr = ["UIKit", "Foundation", "import"]

    full_path = pwd + "/" + file
    im_file = Path(full_path)
    im_string = ""
    if im_file.is_file():
        with open(file, "r") as f:
            im_string = f.read()
    im_array = im_string.split("import")
    im_array = list(filter(None, im_array))
    im_array = list(set(im_array))

    # 删除空格和尾部的换行符
    clear_arr = list()
    for item in im_array:
        item = item.strip().replace("\n", "")
        clear_arr.append(item)

    # 删除不要的元素
    for item in exclude_arr:
        if item in clear_arr:
            clear_arr.remove(item)
    # 格式化增加 ""
    data_source = list()
    for item in clear_arr:
        data = item.strip()
        value = """\"{}\"""".format(data)
        data_source.append(value)


    if mode == "pod":

        pod_list = list()
        for item in data_source:
            value = "pod " + item
            pod_list.append(value)
        pod_file = pwd + "/pod.txt"
        make_file(pod_file, pod_list)
    elif mode == "dependency":

        pod_list = list()
        for item in data_source:
            value = "s.dependency " + item
            pod_list.append(value)

        pod_file = pwd + "/dependency.txt"
        make_file(pod_file, pod_list)
    else:

        pod_list = list()

        for item in data_source:
            value = "pod " + item
            pod_list.append(value)

        for item in data_source:
            value = "s.dependency " + item
            pod_list.append(value)
        # 创建文件夹
        pod_file = pwd + "/all.txt"
        make_file(pod_file, pod_list)
    click.echo("you are success")

if __name__ == "__main__":
    imp2pod()
